#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""  
@Project : wupeiqi_async
@File : 12-连接多个Redis.py
@Author : Royce
@Time : 2025/7/13 15:33  
@脚本说明 : 
"""
import asyncio
import aioredis

from util.util import timer


@timer('ms')
async def execute(address, password):
    print("开始执行", address)

    # 网络IO操作：先去连接address，遇到IO则自动切换任务，再去连接另一个address 的Redis
    redis = await aioredis.from_url(
        url=address, password=password, db=0, decode_responses=True
    )

    # 网络IO操作：遇到IO会自动切换任务
    await redis.hset('car', mapping={'key1': 'Reus', 'key2': 'Curry'})

    # 网络IO操作：遇到IO会自动切换任务
    result = await redis.hgetall('car')

    print(result)
    # 网络IO操作：遇到IO会自动切换任务
    await redis.close()
    print("结束", address)


@timer('ms')
def test():
    task_list = [
        execute('redis://192.168.88.66:6379', "666888"),
        execute('redis://192.168.88.66:6379', "666888"),
        execute('redis://192.168.88.66:6379', "666888"),
        execute('redis://192.168.88.66:6379', "666888"),
    ]
    asyncio.run(asyncio.wait(task_list))


if __name__ == '__main__':
    test()
